
import os
import json
import pandas as pd



def step1_append_ext():
    df1 = pd.read_excel('temp/a01_all_word.xlsx')
    df2 = pd.read_excel('word_basic.xlsx')
    df2.dropna(subset=['en'], inplace=True)
    en_len = []

    # 保证长字符优先保留
    for en in df2['en'].astype(str):
        en_len.append(len(en))
    df2['en_len'] = en_len

    df2 =df2.sort_values('en_len', ascending=False)
    df2 = df2.reset_index(drop=True)


    # 词根字典
    a_dict = dict(zip(df2['en'], df2['cn']))
    b_dict = dict(zip(df2['en'], df2['位置']))


    start_dict = {}
    end_dict = {}
    all_dict = {}
    for i,r in df1[:].astype(str).iterrows():
        word = r['word']
        if len(word)<=2:continue
        # print(word)
        find_ext = []
        for en, cn in a_dict.items():
            en =str(en)
            pass_this_one = False
            for has_find in find_ext:
                if en in has_find:
                    pass_this_one = True
                    break
            if pass_this_one:continue
            if b_dict[en] == 's' and word.startswith(en):
                start_dict[word] = f"{en}"
                find_ext.append(en)
            if b_dict[en] == 'e' and word.endswith(en):
                end_dict[word] = f"{en}"
                find_ext.append(en)
            if b_dict[en] == 'm' and en in word:
                all_dict[word] = f"{en}"
                find_ext.append(en)

    # 应用这个函数到df1的'word'列
    df1['start'] = df1['word'].map(start_dict)
    df1['middle'] = df1['word'].map(all_dict)
    df1['end'] = df1['word'].map(end_dict)
    df1 =df1.sort_values('word').reset_index(drop=True)

    df1.to_excel('temp/s02_words_record.xlsx', index=False)







def run():
    # load_one()
    step1_append_ext()

    pass


if __name__=='__main__':
    run()